/**
 * 多进程与文件缓存的关系
 * gcc -g -rdynamic -I include    -O0 -Wall  -o obj/progress_fork4 src/progress_fork4.c
 * obj/progress_fork4
 * cat s.txt
 *   start: 7268, Hello Gino I am parent progress id is 7268
 *   start: 7268, Hello Gino I am child progress id is 7269
*/

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <unistd.h>

int main()
{

    FILE *fp = fopen("s.txt", "w+");
    if (fp == NULL)
    {
        perror("fopen error : ");
            exit(EXIT_FAILURE);
    }
    fprintf(fp, "start: %d, Hello Gino ", getpid());

    pid_t pid = fork();
    if (0 == pid)
    { // 子进程
        fprintf(fp, "I am child progress id is %d\n", getpid());
    }
    else if (0 < pid)
    { //父进程
        fprintf(fp, "I am parent progress id is %d\n", getpid());
    }
    else
    {
        perror("fork error: ");
        exit(EXIT_FAILURE);
    }

    fclose(fp);
    return 0;
}
